//----------------------------------Include-------------------------------------
#include <string.h>
#include "queue.h"
//------------------------------------------------------------------------------

//-----------------------------------Macro--------------------------------------
//------------------------------------------------------------------------------

//--------------------------------Static Var------------------------------------
static uint32_t     shadow[QUEUE_MAX];
//------------------------------------------------------------------------------

//--------------------------------Static Func-----------------------------------
//------------------------------------------------------------------------------

//--------------------------------Public Func-----------------------------------
//write one
void queue_wr_one(t_queue* pq, uint32_t data)
{
  pq->cap[pq->tail] = data;
  pq->tail++;
  if(pq->tail >= QUEUE_MAX){
    pq->tail = 0;
    pq->full = 1;
  }
}

//delete
void queue_delete_all(t_queue* pq)
{
  pq->tail = 0;
  pq->full = 0;
}

//total
uint32_t queue_total(t_queue* pq)
{
  return pq->full ? QUEUE_MAX : pq->tail;
}

//get max times value
void queue_get_max(t_queue* pq, uint32_t* pmaxv, uint32_t* pidx, uint32_t* ptimes)
{
  uint32_t  i, j, num, t;
  num = pq->full ? QUEUE_MAX : pq->tail;
  memset(shadow, 0, sizeof(shadow));
  for(i=0; i<num; i++){
    for(j=i+1; j<num; j++){
      if(pq->cap[i]==pq->cap[j]) shadow[i]++;
    }
  }
  for(i=1, t=0; i<num; i++){
    if(shadow[i] > shadow[t]) t=i;
  }
  *pidx  = t;
  *pmaxv = pq->cap[t];
  *ptimes= shadow[t]+1;
}

//find specify value    0--not  1--yes
uint32_t queue_find_value(t_queue* pq, uint32_t v, uint32_t* pidx)
{
  uint32_t  i, num, res;
  num = pq->full ? QUEUE_MAX : pq->tail;
  for(i=0, res=0; i<num; i++){
    if(v == pq->cap[i]){ *pidx=i; res=1; break;}
  }
  return res;
}

//average
uint32_t queue_get_average(t_queue* pq)
{
  uint32_t  i, num, sum;
  num = pq->full ? QUEUE_MAX : pq->tail;
  for(i=0, sum=0; i<num; i++){
    sum += pq->cap[i];
  }
  return num ? sum/num : 0;
}
//-----------------------------------------------------------------------------

//------------------------------------EOF--------------------------------------















